<html>
<head><meta charset="utf-8"><title>bound lifetime of child struct · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html">bound lifetime of child struct</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219722780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219722780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> devsnek <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219722780">(Dec 12 2020 at 19:14)</a>:</h4>
<p>imagine these definitions:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">struct</span> <span class="nc">Parent</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Child</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">some</span><span class="w"> </span><span class="n">fields</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// does not contain any references to Parent!!!</span>
<span class="k">impl</span><span class="w"> </span><span class="n">Parent</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">fn</span> <span class="nf">create_child</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Child</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>is there a way to tell the language that a Child should not outlive the Parent that created it? for example:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="kd">let</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Parent</span><span class="w"> </span><span class="p">{};</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">parent</span><span class="p">.</span><span class="n">create_child</span><span class="p">();</span><span class="w"></span>

<span class="n">p</span><span class="p">.</span><span class="n">field</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="c1">// parent is still usable for immutable and mutable borrows</span>

<span class="nb">drop</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="c1">// compile-time error to access `c` here</span>
</code></pre></div>



<a name="219723883"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219723883" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219723883">(Dec 12 2020 at 19:28)</a>:</h4>
<p>You can store a <code>PhantomData&lt;&amp;'a Parent&gt;</code> in the child</p>



<a name="219723896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219723896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Steven Fackler <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219723896">(Dec 12 2020 at 19:28)</a>:</h4>
<p>oh, but that would prevent mutable borrows of the parent</p>



<a name="219723931"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219723931" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> devsnek <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219723931">(Dec 12 2020 at 19:29)</a>:</h4>
<p><span class="user-mention silent" data-user-id="243558">Steven Fackler</span> <a href="#narrow/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct/near/219723896">said</a>:</p>
<blockquote>
<p>oh, but that would prevent mutable borrows of the parent</p>
</blockquote>
<p>yeah that's the part i haven't been able to figure out...</p>



<a name="219738438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219738438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219738438">(Dec 13 2020 at 01:45)</a>:</h4>
<p>Isn't this isomorphic to scoped threads?</p>



<a name="219738712"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219738712" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219738712">(Dec 13 2020 at 01:54)</a>:</h4>
<p>Something like:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">marker</span>::<span class="n">PhantomData</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Parent</span><span class="p">;</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Scope</span><span class="p">(());</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Scope</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">create_child</span><span class="o">&lt;'</span><span class="na">s</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">s</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nc">Child</span><span class="o">&lt;'</span><span class="na">s</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Child</span><span class="p">(</span><span class="n">PhantomData</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Child</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">PhantomData</span><span class="o">&lt;&amp;'</span><span class="na">a</span><span class="w"> </span><span class="n">Scope</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Parent</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">scope</span><span class="o">&lt;'</span><span class="na">p</span><span class="p">,</span><span class="w"> </span><span class="n">F</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">p</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">f</span>: <span class="nc">F</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="k">where</span><span class="w"></span>
<span class="w">        </span><span class="n">F</span>: <span class="nc">for</span><span class="o">&lt;'</span><span class="na">s</span><span class="o">&gt;</span><span class="w"> </span><span class="nb">FnOnce</span><span class="p">(</span><span class="o">&amp;'</span><span class="na">p</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">Parent</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">s</span><span class="w"> </span><span class="n">Scope</span><span class="p">),</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Scope</span><span class="p">(());</span><span class="w"></span>
<span class="w">        </span><span class="n">f</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">s</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">example</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">example2</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Parent</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">p</span><span class="p">.</span><span class="n">scope</span><span class="p">(</span><span class="o">|</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">creator</span><span class="o">|</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">_c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">creator</span><span class="p">.</span><span class="n">create_child</span><span class="p">();</span><span class="w"></span>

<span class="w">        </span><span class="c1">// parent is still usable for immutable and mutable borrows</span>
<span class="w">        </span><span class="n">p</span><span class="p">.</span><span class="n">example</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">p</span><span class="p">.</span><span class="n">example2</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="p">})</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="219759371"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219759371" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219759371">(Dec 13 2020 at 12:18)</a>:</h4>
<p>Callback patterns are indeed very powerful for these things; btw, with a bit of self-plug, let's see how that reads using <a href="https://docs.rs/with_locals">https://docs.rs/with_locals</a></p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">impl</span><span class="w"> </span><span class="n">Parent</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[with('local)]</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">scope</span><span class="o">&lt;'</span><span class="na">scope</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">scope</span> <span class="nc">mut</span><span class="w"> </span><span class="bp">Self</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="p">(</span><span class="o">&amp;'</span><span class="na">scope</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="bp">Self</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">local</span><span class="w"> </span><span class="n">Scope</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Scope</span><span class="p">(()))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">fn</span> <span class="nf">example</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">example2</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="bp">self</span><span class="p">)</span><span class="w"> </span><span class="p">{}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="cp">#[with]</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Parent</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="cp">#[with]</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">creator</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">scope</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">creator</span><span class="p">.</span><span class="n">create_child</span><span class="p">();</span><span class="w"></span>

<span class="w">    </span><span class="c1">// parent is still usable for immutable and mutable borrows</span>
<span class="w">    </span><span class="n">p</span><span class="p">.</span><span class="n">example</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">condition</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="k">return</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// Just Works.</span>
<span class="w">    </span><span class="n">p</span><span class="p">.</span><span class="n">example2</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="219832472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219832472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219832472">(Dec 14 2020 at 12:14)</a>:</h4>
<p>Actually, <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span>, in this instance, we can skip the whole callback ordeal:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span>::<span class="n">core</span>::<span class="p">{</span><span class="n">marker</span>::<span class="n">PhantomData</span><span class="p">,</span><span class="w"> </span><span class="n">mem</span>::<span class="nb">drop</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="n">stuff</span><span class="p">};</span><span class="w"></span>

<span class="k">struct</span> <span class="nc">Parent</span><span class="p">;</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Scope</span><span class="p">(());</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Child</span><span class="o">&lt;'</span><span class="na">a</span><span class="o">&gt;</span><span class="p">(</span><span class="n">PhantomData</span><span class="o">&lt;&amp;'</span><span class="na">a</span><span class="w"> </span><span class="n">Scope</span><span class="o">&gt;</span><span class="p">);</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Scope</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">create_child</span><span class="o">&lt;'</span><span class="na">scope</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">scope</span> <span class="nc">Scope</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span>-&gt; <span class="nc">Child</span><span class="o">&lt;'</span><span class="na">scope</span><span class="o">&gt;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Child</span><span class="p">(</span><span class="n">PhantomData</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="n">Parent</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">scope</span><span class="o">&lt;'</span><span class="na">scope</span><span class="o">&gt;</span><span class="w"> </span><span class="p">(</span><span class="bp">self</span>: <span class="kp">&amp;</span><span class="o">'</span><span class="na">scope</span> <span class="nc">mut</span><span class="w"> </span><span class="n">Parent</span><span class="p">)</span><span class="w"></span>
<span class="w">      </span>-&gt; <span class="p">(</span><span class="o">&amp;'</span><span class="na">scope</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">Parent</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;'</span><span class="na">scope</span><span class="w"> </span><span class="n">Scope</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Scope</span><span class="p">(()))</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="w"> </span><span class="p">()</span><span class="w"></span>
<span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_c</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">p</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Parent</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="p">(</span><span class="n">p</span><span class="p">,</span><span class="w"> </span><span class="n">creator</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">p</span><span class="p">.</span><span class="n">scope</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="n">_c</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">creator</span><span class="p">.</span><span class="n">create_child</span><span class="p">();</span><span class="w"></span>

<span class="w">        </span><span class="c1">// parent is still usable for immutable and mutable borrows</span>
<span class="w">        </span><span class="n">stuff</span>::<span class="o">&lt;&amp;</span><span class="n">Parent</span><span class="o">&gt;</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="n">stuff</span>::<span class="o">&lt;&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">Parent</span><span class="o">&gt;</span><span class="p">(</span><span class="n">p</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="cm">/* Uncomment to get an error */</span><span class="w"></span>
<span class="w">    </span><span class="c1">// drop(_c);</span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<ul>
<li><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=691165b783b9f67820d58b1a00590f29">Playground</a></li>
</ul>



<a name="219832914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219832914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219832914">(Dec 14 2020 at 12:18)</a>:</h4>
<p>Obviously the issue in these instances is that you cannot ensure that a given <code>&amp;'s Scope</code> originates from a specific <code>Parent</code>, imagining a <code>Child</code> constructor along the lines of <code>fn new_child&lt;'s&gt; (&amp;'_ Parent, &amp;'s Scope) -&gt; Child&lt;'s&gt;</code>.<br>
In that case, one needs to use lifetime generativity: infect each <code>Parent</code> (or a wrapper of it) with a unique invariant lifetime (which requires callbacks and/or macros), and then one can do stuff like <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=c51a4ef5af751e09d89fda612bd2af06">this</a>.</p>



<a name="219836166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219836166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219836166">(Dec 14 2020 at 12:48)</a>:</h4>
<p>Do you need the <code>Scope</code> struct? Wouldn't<code>fn new_child&lt;'scope&gt;(&amp;'scope mut Parent) -&gt; (&amp;'scope mut Parent, Child&lt;'scope&gt;)</code> work? The trick is the re-outputting of the <code>&amp;mut</code> ref, I don't think you need more than that</p>



<a name="219837220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219837220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nadrieril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219837220">(Dec 14 2020 at 12:58)</a>:</h4>
<p>I also want to drop this here, it's insane but elegant: <a href="https://internals.rust-lang.org/t/idea-two-lifetimes-on-unique-references/10613">https://internals.rust-lang.org/t/idea-two-lifetimes-on-unique-references/10613</a></p>



<a name="219845744"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/bound%20lifetime%20of%20child%20struct/near/219845744" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/bound.20lifetime.20of.20child.20struct.html#219845744">(Dec 14 2020 at 14:12)</a>:</h4>
<p><span class="user-mention" data-user-id="245339">@Nadrieril</span> indeed the <code>Scope</code> intermediate stuff is not required <em>per se</em>, it's just convenient when creating multiple children and stuff. But for one-time calls, indeed the whole thing can be simplified <span aria-label="100" class="emoji emoji-1f4af" role="img" title="100">:100:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>